import axios from 'axios';
import Cookies from 'js-cookie'


const service = axios.create({
    // process.env.NODE_ENV === 'development' 来判断是否开发环境
    // easy-mock服务挂了，暂时不使用了
    // baseURL: 'https://www.easy-mock.com/mock/592501a391470c0ac1fab128',
    timeout: 60000,
    crossDomain: true
});

service.interceptors.request.use(
    config => {
        config.headers['token'] = Cookies.get('token') || ''
        // config.headers['authkey'] = Cookies.get('token') || '',

		config.headers['domainname'] = Cookies.get('domainname') || ''
		config.headers['fronttoken'] = Cookies.get('fronttoken') || ''
        return config;
    },
    error => {
        console.log(error);
        return Promise.reject();
    }
);

service.interceptors.response.use(
    response => {
        if (response.status === 200) {
            if ((typeof response.data == 'string') && response.data.constructor == String) {//如果是字符串要看是否为json字符串，是就转换对象
                try{
                    let data=eval('('+response.data+')'); // 如果没抛出异常，说明是json字符串则直接转
                    return data;
                }catch(e){
                    return response.data;// 如果抛出异常，说明是不同字符，直接返回就行
                 }
               }else{
                return response.data;
               }
        } else {
            Promise.reject();
        }
    },
    error => {
        console.log(error);
        return Promise.reject();
    }
);

export default service;
